<?php
/**
 * User: 五哥派
 * Date: 2024/7/1
 * Tel(同WX): 17335592719
 */
namespace app\api\controller\v1;

use app\api\controller\ApiBase;
use app\api\model\store\Goods as GoodsModel;
use app\api\model\store\GoodsSku as GoodsSkuModel;
use app\api\service\SpecsValue as SpecsValueService;

class Goods extends ApiBase {

    protected $model;
    protected $mGoodsSkuModel;

    public function __construct(GoodsModel $model, GoodsSkuModel $mGoodsSkuModel){
        parent::__construct();
        $this->model = $model;
        $this->mGoodsSkuModel = $mGoodsSkuModel;
    }

    public function getListByCategoryId(){
        $category_id = $this->request->param('category_id', 0);

        $list = $this->model->queryData(['status' => 1, 'is_hot' => 1, 'category_id' => $category_id], 'id,sku_id,name,image');
        if (empty($list)) {
            return error('查询失败');
        }
        return success('查询成功', ['list' => $list]);
    }

    public function getList(){
        $is_hot = $this->request->param('is_hot', -1);
        $category_id = $this->request->param('category_id', -1);
        $sales = $this->request->param('sales', 'asc');
        $price = $this->request->param('price', 'desc');
        $where['status'] = 1;
        if($is_hot > 0){
            $where['is_hot'] = $is_hot;
        }
        if($category_id > 0){
            $where['category_id'] = $category_id;
        }

        //"id" => "desc", "create_time" => "desc",
        $order = ["sales" => $sales, "sell_price" => $price];

        //dump($where);
        //dump($order);
        //die;

        $list = $this->model->queryData($where, 'id,sku_id,category_id,name,image,is_hot,sell_price,market_price,sales,status,create_time', $order);
        if (empty($list)) {
            return error('查询失败');
        }
        return success('查询成功', ['list' => $list]);
    }

    public function getDetail(){
        $skuId = $this->request->param('id', 0);//sku_id
        $goodsSku = $this->mGoodsSkuModel->with("goods")->where('id',$skuId)->find();
        if(!$goodsSku) {
            return [];
        }
        if(empty($goodsSku['goods'])) {
            return [];
        }
        $goods = $goodsSku['goods'];
        $goodsId = $goods['id'];
        $where1 = [
            "goods_id" => $goodsId,
            "status" => 1,
        ];
        $skus = $this->mGoodsSkuModel->where($where1)->select();
        if(!$skus) {
            return [];
        }
        $flagValue = "";
        foreach($skus as $sv) {
            if($sv['id'] == $skuId) {
                $flagValue = $sv["specs_value_ids"];
            }
        }

        //$gids = array_column($skus, "id", "specs_value_ids");
        $gids = array_column($skus->toArray(), "specs_value_ids", "id");

        if($goods['specs_type'] == 1) {
            $sku = [];
        } else {
            $sku = (new SpecsValueService())->dealGoodsSkus($gids, $flagValue);
        }

        $result = [
            "title" => $goods['name'],
            "unit_name" => $goods['unit_name'],
            "specs_type" => $goods['specs_type'],
            "sell_price" => $goodsSku['sell_price'],
            "market_price" => $goodsSku['market_price'],
            "sales_count" =>  $goods['sales'],
            "stock" => $goodsSku['stock'],
            "gids" => $gids,
            "image" => $goods['carousel_image'],
            "sku" => $sku,
            "detail" => [
                "d1" => [
                    "商品编码" => $goodsSku['id'],
                    "上架时间" => $goods['create_time'],
                ],
                //"d2" => preg_replace('/(<img.+?src=")(.*?)/', '$1'.request()->domain().'$2',$goods['content']),
                "d2" => $goods['content'],
            ],
        ];

        if (!$result) {
            return error('查询失败');
        }
        return success('查询成功', ['detail' => $result]);
    }


}